// pages/changeAdmin/changeAdmin.js
Page({
  data: {
    user: {},
    oldPwd: '',
    newPwd: '',
    checkPwd: '',
    eyeType: true,
    passwordType: true,
    eyeType2: true,
    passwordType2: true,
    eyeType3: true,
    passwordType3: true,
  },
  //密码是否可见
  eyeStatus() {
    this.setData({
      passwordType: !this.data.passwordType,
      eyeType: !this.data.eyeType,
    })
  },
  eyeStatus2() {
    this.setData({
      passwordType2: !this.data.passwordType2,
      eyeType2: !this.data.eyeType2,
    })
  },
  eyeStatus3() {
    this.setData({
      passwordType3: !this.data.passwordType3,
      eyeType3: !this.data.eyeType3,
    })
  },
  //一进来的时候获取数据
  onLoad(options) {
    var that = this
    var user = JSON.parse(options.user)
    //一进来就获取这个人的账号
    that.setData({
      user: user
    })
  },
  //获取旧密码
  getOld(e) {
    console.log(e.detail.value)
    this.setData({
      oldPwd: e.detail.value
    })
  },
  //获取新密码
  getnewPwd(e) {
    console.log(e.detail.value)
    this.setData({
      newPwd: e.detail.value
    })
  },
  //获取确认密码
  getcheckPwd(e) {
    console.log(e.detail.value)
    this.setData({
      checkPwd: e.detail.value
    })
  },
  //提交修改密码
  commit() {
    var that = this
    //提示正在修改
    wx.showLoading({
      title: '密码修改中...',
    })
    //上云函数拿user数据
    wx.cloud.callFunction({
      name: "user",
      data: {
        action: "find",
        admin: that.data.user.admin,
      }
    }).then(res => {
      console.log('获取云数据库user信息成功(密码)', res.result.data[0].pwd)
      //验证旧密码是否正确
      if (that.data.oldPwd != res.result.data[0].pwd) {
        wx.showToast({
          icon: 'none',
          title: '原密码错误',
        })
        return
      }
      //验证新密码是大于六位
      if (that.data.newPwd.length < 6) {
        wx.showToast({
          icon: 'none',
          title: '密码不能少于6个字符',
        })
        return
      }
      //验证两个密码是否一致
      if (that.data.newPwd != that.data.checkPwd) {
        wx.showToast({
          icon: 'none',
          title: '两次密码输入不一致',
        })
        return
      }
      //验证都通过，则通过云函数修改密码
      wx.cloud.callFunction({
        name: 'user',
        data: {
          action: 'change',
          admin: that.data.user.admin,
          pwd: that.data.newPwd
        }
      }).then(res => {
        console.log('修改密码成功', res)
        //关闭提示框
        wx.hideLoading({
          success: (res) => {
            wx.showToast({
              title: '修改密码成功',
            })
          },
        })
        //退出登录，跳转到登录页面（延时）
        setTimeout(() => {
          //退出
          console.log("退出小程序")
          wx.navigateTo({
            url: '/pages/login/login',
          })
          wx.setStorageSync('user', null)
          setTimeout(() => {
            wx.showToast({
              icon:'none',
              title: '请重新登录',
            })
          }, 1000);
        }, 1000);
      }).catch(res => {
        console.log('修改密码失败', res)
        //关闭提示框
        wx.hideLoading({
          success: (res) => {
            wx.showToast({
              icon: 'none',
              title: '修改密码失败',
            })
          },
        })
      })

    }).catch(res => {
      console.log('获取云数据库user信息失败', res)
      //关闭提示框
      wx.hideLoading({
        success: (res) => {
          wx.showToast({
            icon: 'none',
            title: '修改密码失败',
          })
        },
      })
    })

  }

})